package com.pallass.common.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.pallass.utils.DatePropertyEditor;

public class BaseController {

	private static Logger logger = LoggerFactory.getLogger(BaseController.class);

	@Autowired
	protected HttpServletRequest request;

	@Autowired
	protected HttpServletResponse response;

	@InitBinder
	protected void initBinder(WebDataBinder webDataBinder) {
		webDataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
		webDataBinder.registerCustomEditor(Date.class, new DatePropertyEditor(true));
	}

	/**
	 * 带参数重定向
	 *
	 * @param path
	 * @return
	 */
	protected String redirect(String path) {
		return "redirect:" + path;
	}

	/**
	 * 无参数重定向
	 *
	 * @param response
	 * @param path
	 * @return
	 */
	protected String redirect(HttpServletResponse response, String path) {
		try {
			response.sendRedirect(path);
		} catch (IOException e) {
			logger.info("redirect error.");
		}
		return null;
	}

	/**
	 * 获取分页请求
	 * 
	 * @return
	 */
	protected PageRequest getPageRequest() {
		int page = 1;
		int size = 10;
		Sort sort = null;
		try {
			String sortName = request.getParameter("sortName");
			String sortOrder = request.getParameter("sortOrder");
			if (StringUtils.isNoneBlank(sortName) && StringUtils.isNoneBlank(sortOrder)) {
				if ("desc".equalsIgnoreCase(sortOrder)) {
					sort = new Sort(Direction.DESC, sortName);
				} else {
					sort = new Sort(Direction.ASC, sortName);
				}
			}
			page = Integer.parseInt(request.getParameter("pageNumber")) - 1;
			size = Integer.parseInt(request.getParameter("pageSize"));
		} catch (NumberFormatException e) {
			logger.info("pageNumber or pageSize is not number.");
		}
		return new PageRequest(page, size, sort);
	}

	/**
	 * 获取分页请求
	 * 
	 * @param sort
	 *            排序条件
	 * @return
	 */
	protected PageRequest getPageRequest(Sort sort) {
		int page = 0;
		int size = 10;
		try {
			String sortName = request.getParameter("sortName");
			String sortOrder = request.getParameter("sortOrder");
			if (StringUtils.isNoneBlank(sortName) && StringUtils.isNoneBlank(sortOrder)) {
				if ("desc".equalsIgnoreCase(sortOrder)) {
					sort.and(new Sort(Direction.DESC, sortName));
				} else {
					sort.and(new Sort(Direction.ASC, sortName));
				}
			}
			page = Integer.parseInt(request.getParameter("pageNumber")) - 1;
			size = Integer.parseInt(request.getParameter("pageSize"));
		} catch (NumberFormatException e) {
			logger.info("pageNumber or pageSize is not number.");
		}
		return new PageRequest(page, size, sort);
	}

}
